/**
 * 全站路由配置
 *
 * 建议:
 * 1. 代码中路由统一使用name属性跳转(不使用path属性)
 */
import Vue from 'vue'
import Router from 'vue-router'
import { clearLoginInfo } from '@/utils'

Vue.use(Router)

// 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
const _import = require('./import-' + process.env.NODE_ENV)

// 全局路由(无需嵌套上左右整体布局)
const globalRoutes = [
  { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
  { path: '/login', component: _import('common/login'), name: 'login', meta: { title: '登录' } }
]

// 主入口路由(需嵌套上左右整体布局)
const mainRoutes = {
  path: '/',
  component: _import('main'),
  name: 'main',
  redirect: { name: 'home' },
  meta: { title: '主入口整体布局' },
  children: [
    // 通过meta对象设置路由展示方式
    // 1. isTab: 是否通过tab展示内容, true: 是, false: 否
    // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
    // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
    { path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } },
    { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } },
    { path: '/tenant/role', component: _import('modules/tenant/role'), name: 'tenant-role', meta: { title: '租户角色', isTab: true } },
    { path: '/tenant/list', component: _import('modules/tenant/tenant'), name: 'tenant-list', meta: { title: '租户列表', isTab: true } },
    { path: '/tenant/user', component: _import('modules/tenant/user'), name: 'tenant-user', meta: { title: '用户列表', isTab: true } },
    { path: '/sys/user', component: _import('modules/sys/user'), name: 'sys-user', meta: { title: '管理员列表', isTab: true } },
    { path: '/sys/role', component: _import('modules/sys/role'), name: 'sys-role', meta: { title: '角色列表', isTab: true } },
    { path: '/sys/menu', component: _import('modules/sys/menu'), name: 'sys-menu', meta: { title: '菜单管理', isTab: true } },
    { path: '/oss/oss', component: _import('modules/oss/oss'), name: 'oss-oss', meta: { title: '文件上传', isTab: true } },
    { path: '/job-schedule', component: _import('modules/job/schedule'), name: 'job-schedule', meta: { title: '定时任务', isTab: true } },
    { path: '/sys-dict', component: _import('modules/sys/dict'), name: 'sys-dict', meta: { title: '字典管理', isTab: true } },
    { path: '/sys-module', component: _import('modules/sys/module'), name: 'sys-module', meta: { title: '模块管理', isTab: true } },
    { path: '/sys-config', component: _import('modules/sys/config'), name: 'sys-config', meta: { title: '参数管理', isTab: true } },
    { path: '/sys-adposition', component: _import('modules/sys/adposition'), name: 'sys-adposition', meta: { title: '广告位置', isTab: true } },
    { path: '/sys-log', component: _import('modules/sys/log'), name: 'sys-log', meta: { title: '系统日志', isTab: true } },
    { path: '/user-list', component: _import('modules/user/member'), name: 'user-list', meta: { title: '用户列表', isTab: true } },
    { path: '/user-recovery', component: _import('modules/user/user-recovery'), name: 'user-recovery', meta: { title: '用户列表', isTab: true } },
    { path: '/integral', component: _import('modules/integral/integral'), name: 'integral', meta: { title: '积分管理', isTab: true } },
    { path: '/integral/classification', component: _import('modules/integral/classification'), name: 'integral-classification', meta: { title: '积分分类', isTab: true } }
  ],
  beforeEnter(to, from, next) {
    let token = Vue.cookie.get('admintoken')
    if (!token || !/\S/.test(token)) {
      clearLoginInfo()
      next({ name: 'login' })
    }
    next()
  }
}

const router = new Router({
  mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  routes: globalRoutes.concat(mainRoutes)
})

router.beforeEach((to, from, next) => {
  // 添加动态(菜单)路由
  // 1. 已经添加 or 全局路由, 直接访问
  // 2. 获取菜单列表, 添加并保存本地存储
  if (fnCurrentRouteType(to, globalRoutes) === 'global') {
    next()
  } else {
    mainRoutes.beforeEnter(to, from, next)
  }
})

/**
 * 判断当前路由类型, global: 全局路由, main: 主入口路由
 * @param {*} route 当前路由
 */
function fnCurrentRouteType(route, globalRoutes = []) {
  var temp = []
  for (var i = 0; i < globalRoutes.length; i++) {
    if (route.path === globalRoutes[i].path) {
      return 'global'
    } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
      temp = temp.concat(globalRoutes[i].children)
    }
  }
  return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
}

export default router
